package com.jianyun.jyzs.adapter;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.jianyun.jyzs.R;
import com.jianyun.jyzs.activity.SelectEProjectActivity;
import com.jianyun.jyzs.bean.EngineerProjectResult;
import com.jianyun.jyzs.constant.SysConstant;
import com.jianyun.jyzs.dao.EngProjectDao;
import com.jrmf360.rylib.common.util.ToastUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by mlli on 2017/11/8.
 */

public class SelectP1Adapter extends RecyclerView.Adapter {

    private SelectEProjectActivity context;
    private List<EngineerProjectResult.EngProject> list;
    private final EngProjectDao dao;

    public SelectP1Adapter(SelectEProjectActivity context, List<EngineerProjectResult.EngProject> list) {
        this.context = context;
        this.list = list;
        dao = new EngProjectDao(context);
    }

    public void setList(List<EngineerProjectResult.EngProject> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.p_select_item, parent, false);
        return new MHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final MHolder mHolder = (MHolder) holder;
        final EngineerProjectResult.EngProject project = list.get(position);
        if (project.isAll()) {
            mHolder.rlPart2.setVisibility(View.INVISIBLE);
        }
        mHolder.title.setText(project.getProJectName());
        mHolder.rlPart1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mHolder.checkBox.setChecked(true);
                int count = project.getCount();
                project.setCount(count + 1);
                dao.insert(project);
                Intent intent = new Intent();
                intent.putExtra(SysConstant.msg_p1, project);
                context.setResult(-1, intent);
                context.finish();
            }
        });
        mHolder.rlPart2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                ToastUtil.showNoWaitToast(context, "已选择" + project.getProJectName() + "进入下一级");
                int autoId = project.getAutoId();
                //提示确认选项
                if (selectNextLevelListener != null) selectNextLevelListener.toNextLevel(project);

            }
        });

    }

    private SelectNextLevelListener selectNextLevelListener;

    public void setSelectNextLevelListener(SelectNextLevelListener selectNextLevelListener) {
        this.selectNextLevelListener = selectNextLevelListener;
    }

    public interface SelectNextLevelListener {
        void toNextLevel(EngineerProjectResult.EngProject engProject);
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    class MHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.checkBox)
        CheckBox checkBox;
        @BindView(R.id.title)
        TextView title;
        @BindView(R.id.rl_part1)//点击选择项目返回动态首页展示此动态类型所有动态
                LinearLayout rlPart1;
        @BindView(R.id.rl_part2)
        TextView rlPart2;
        @BindView(R.id.line_bottom)
        View lineBottom;

        public MHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
